//
//  TravelDiaryViewController.m
//  NineGroupProject
//
//  Created by dllo on 15/12/23.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "TravelDiaryViewController.h"
#import "MJRefresh.h"
#import "LSAppTools.h"
#import "DiaryModel.h"
#import "DiaryTableViewCell.h"
#import "TravelDiaryDetailViewController.h"
@interface TravelDiaryViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *myTableView;
@property (nonatomic, strong) NSMutableArray *array;
@property (nonatomic, assign) BOOL isUpLoad;
@property (nonatomic, assign) NSInteger page;
@end

@implementation TravelDiaryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIImage *leftImage = [UIImage imageNamed:@"iconfont-fanhui-2.png"];
    leftImage = [leftImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:leftImage style:UIBarButtonItemStyleDone target:self action:@selector(leftButtonAction)];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = [NSString stringWithFormat:@"%@旅行地", self.roadModel.name];
    
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 66 * HEI) style:UITableViewStylePlain];
    [self.view addSubview:self.myTableView];

    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    
    [self.myTableView registerClass:[DiaryTableViewCell class] forCellReuseIdentifier:@"DiaryTableViewCell"];
    
    
    [self addHeaderRefresh];
    [self addFooterRefresh];
    

    
    self.array = [NSMutableArray array];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)addHeaderRefresh
{
    
    
    // 设置回调（一旦进入刷新状态，就调用target的action，也就是调用self的loadNewData方法）
    MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(getAFNetWorkingData)];
    
    NSMutableArray *array = [NSMutableArray array];
    for (int i = 1; i < 52; i++) {
        NSString *string = [NSString stringWithFormat:@"a18d7359gw1ewdbb5kj49g20qo02fmyr-2－%d.tiff", i];
        UIImage *image = [UIImage imageNamed:string];
        [array addObject:image];
    }
    // 设置普通状态的动画图片
//    [header setImages:idleImages forState:MJRefreshStateIdle];
    // 设置即将刷新状态的动画图片（一松开就会刷新的状态）
    [header setImages:array forState:MJRefreshStatePulling];
    // 设置正在刷新状态的动画图片
//    [header setImages:refreshingImages forState:MJRefreshStateRefreshing];
    // 设置header
    // 隐藏时间
    header.lastUpdatedTimeLabel.hidden = YES;
    
    // 隐藏状态
    header.stateLabel.hidden = YES;

    self.myTableView.header = header;
    
    [self.myTableView.header beginRefreshing];
}




#pragma mark -- 设置上拉刷新
-(void)addFooterRefresh
{

    self.myTableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        // 进入刷新状态后会自动调用这个block
        
        NSLog(@"上拉");
        self.isUpLoad = YES;
        self.page++;
        // 重新请求数据
        [self getAFNetWorkingData];
    }];
    
}

- (void)getAFNetWorkingData{
    if (self.isUpLoad == NO) {
        
        NSString *url_string = [NSString stringWithFormat:@"http://chanyouji.com/api/destinations/attractions/%@.json?per_page=20&page=1",self.roadModel.nameId];
        [LSAppTools getDataWithURL:url_string blcok:^(id result) {
            [self.array removeAllObjects];
            for (NSMutableDictionary *dic in result) {
                DiaryModel *diaryModel = [[DiaryModel alloc] init];
                [diaryModel setValuesForKeysWithDictionary:dic];
                
                [self.array addObject:diaryModel];
                
            }
            [self.myTableView reloadData];
            
            
        }];
    }else{
        NSString *url_string = [NSString stringWithFormat:@"http://chanyouji.com/api/destinations/attractions/%@.json?per_page=20&page=%ld",self.roadModel.nameId, self.page];
        [LSAppTools getDataWithURL:url_string blcok:^(id result) {
            for (NSMutableDictionary *dic in result) {
                DiaryModel *diaryModel = [[DiaryModel alloc] init];
                [diaryModel setValuesForKeysWithDictionary:dic];
                [self.array addObject:diaryModel];
                
            }

            
        }];
        
    }
    [self.myTableView.header endRefreshing];
    [self.myTableView.footer endRefreshing];
    [self.myTableView reloadData];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.array.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    DiaryTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DiaryTableViewCell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    DiaryModel *diaryModel = [self.array objectAtIndex:indexPath.row];
    cell.diaryModel = diaryModel;
    
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100 * HEI;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    TravelDiaryDetailViewController *diaryDetailVC = [[TravelDiaryDetailViewController alloc] init];
    DiaryModel *diaryModel = [self.array objectAtIndex:indexPath.row];
    diaryDetailVC.diaryModel = diaryModel;
    
    
    
    [self.navigationController pushViewController:diaryDetailVC animated:YES];
    
}
-(void)leftButtonAction{
    [self.navigationController popViewControllerAnimated:YES];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
